﻿ArithmeticTests = function () {
    this.Name = "Arithmetic Tests";
};

ArithmeticTests.prototype = new TestFixture();

ArithmeticTests.prototype.TestAddition = function () {
    var vm = new VM();

    // 1 + 2 + 3 + 4 = 10
    vm.Execute("ldc.i4 1");
    vm.Execute("ldc.i4 2");
    vm.Execute("ldc.i4 3");
    vm.Execute("ldc.i4 4");
    vm.Execute("add");
    vm.Execute("add");
    vm.Execute("add");

    var result = vm.State.Stack.Top();
    this.Assert(10, result.Value);
};

ArithmeticTests.prototype.TestSubtraction = function () {
    var vm = new VM();

    // 20 - (10 - 5) = 15
    vm.Execute("ldc.i4 20");
    vm.Execute("ldc.i4 10");
    vm.Execute("ldc.i4 5");
    vm.Execute("sub");
    vm.Execute("sub");

    var result = vm.State.Stack.Top();
    this.Assert(15, result.Value);
};

ArithmeticTests.prototype.TestCombined = function () {
    var vm = new VM();

    // 20 + 10 - 5 = 25
    vm.Execute("ldc.i4 20");
    vm.Execute("ldc.i4 10");
    vm.Execute("add");
    vm.Execute("ldc.i4 5");
    vm.Execute("sub");

    var result = vm.State.Stack.Top();
    this.Assert(25, result.Value);
};

ArithmeticTests.prototype.TestMultiplication = function () {
    var vm = new VM();

    // 5 * 4 * 2 = 40
    vm.Execute("ldc.i4 5");
    vm.Execute("ldc.i4 4");
    vm.Execute("ldc.i4 2");
    vm.Execute("mul");
    vm.Execute("mul");

    var result = vm.State.Stack.Top();
    this.Assert(40, result.Value);
};

ArithmeticTests.prototype.TestProgram = function () {
    var vm = new VM();
    var commands = [];


    // 5 * 4 * 2 = 40
    commands.push("ldc.i4 5");
    commands.push("ldc.i4 4");
    commands.push("ldc.i4 2");
    commands.push("mul");
    commands.push("mul");

    vm.ExecuteProgram(commands);

    var result = vm.State.Stack.Top();
    this.Assert(40, result.Value);
};


